﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace YATC
{
    public class ActionScene : GameScene
    {

        private GameMatrix gameMatrix;
        private int time,_time;
        private int timeHelper = Environment.TickCount;
        private KeyboardState olds;
        private AudioLibrary audio;
        private TimeSpan musicDuration = TimeSpan.Zero;

        public ActionScene(Game game, SpriteFont font, Model model, Model bord, Model[] shape, Model goText, Model gpText)
            : base(game)
        {
            gameMatrix = new GameMatrix(Game, model, _Components, bord, shape, goText,gpText);
            _Components.Add(gameMatrix);
            olds = Keyboard.GetState();
            
        }

        public override void Initialize()
        {
            gameMatrix.Initialize();
            ((Game1) Game).camera.Initialise();
            time = 0;
            _time = 0;
            timeHelper = Environment.TickCount;
            audio = (AudioLibrary) Game.Services.GetService(typeof (AudioLibrary));
            base.Initialize();
        }
        public bool IsGamePaused
        {
            get{ return gameMatrix._IsGamePaused;}
        }
        public bool IsGameOver
        {
            get{ return gameMatrix._IsGameOver;}
        }
        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (gameMatrix._IsGameOver)
                Score.time = time;
            else

                if (gameMatrix._IsGamePaused || gameMatrix.Translate)
                {
                    _time = time;
                    timeHelper = Environment.TickCount;
                }
                else
                    time = (Environment.TickCount - timeHelper)/1000 + _time;


            HandleInput(dt);
            ((Game1)Game).camera.Update();

            musicDuration += gameTime.ElapsedGameTime;
            if(musicDuration > audio.GetCurrentMusic().Duration)
            {
                musicDuration = TimeSpan.Zero; 
                MediaPlayer.Play(audio.GetNextMusic());
            }
            base.Update(gameTime);
        }
        public override void Hide()
        {
            MediaPlayer.Pause();

            _time = time;
            base.Hide();
        }
        private void HandleInput(float dt)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();
            var padState = GamePad.GetState(PlayerIndex.One);
            Vector2 LeftThumb = padState.ThumbSticks.Left;

            float xDifference = mouseState.X - 200;
            float yDifference = mouseState.Y - 200;
        //  ((Game1) Game).camera.Yaw -= xDifference * .002f;
         //  ((Game1) Game).camera.Pitch -= yDifference * .002f;
            ((Game1)Game).camera._x -= xDifference * .009f;
            ((Game1)Game).camera._y -= yDifference * .009f;

            ((Game1)Game).camera._x -= xDifference * .009f;
            ((Game1)Game).camera._y -= yDifference * .009f;
            Mouse.SetPosition(200, 200);


            //Kamerasteuerung mit  E,D,F,S,A,Z
            if (keyboardState.IsKeyDown(Keys.E))
                ((Game1) Game).camera.MoveForward(dt);
            if (keyboardState.IsKeyDown(Keys.D))
                ((Game1) Game).camera.MoveForward(-dt);
            if (keyboardState.IsKeyDown(Keys.F))
                ((Game1) Game).camera.MoveRight(dt);
            if (keyboardState.IsKeyDown(Keys.S))
                ((Game1) Game).camera.MoveRight(-dt);
            if (keyboardState.IsKeyDown(Keys.A))
                ((Game1) Game).camera.MoveUp(dt);
            if (keyboardState.IsKeyDown(Keys.Y))
                ((Game1) Game).camera.MoveUp(-dt);

            if (keyboardState.IsKeyDown(Keys.N) && olds.IsKeyUp(Keys.N))
            {
                MediaPlayer.Play(audio.GetNextMusic());
                musicDuration = TimeSpan.Zero; 
            }
            if (keyboardState.IsKeyDown(Keys.B) && olds.IsKeyUp(Keys.B))
            {
                MediaPlayer.Play(audio.GetPreviosMusic());
                musicDuration = TimeSpan.Zero; 
            }

            olds = keyboardState;

        }
        public override void Show()
        {
            MediaPlayer.Resume();
        	MediaPlayer.Volume = Score.volume;
            Mouse.SetPosition(200, 200);
            timeHelper = Environment.TickCount;
            base.Show();
        }
        public int Time
        {
            get{ return time;}
        }

       
    }
}
